<?php
session_start();		//启动session
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
	header("location:/login.php");
	eixt();
}
//链接数据库
$link= mysqli_connect("127.0.0.1", "root", "", "demo");

//接收数据
//var_dump($_POST);
if(isset($_POST['username']) && $_POST['username']!=""){
	$username=$_POST['username'];
	$age=$_POST['age'];
	$sex=$_POST['sex'];

	$imagename=$_FILES['image']['name'];		//原文件名
	$tmp_name=$_FILES['image']['tmp_name'];		//临时路径


	//把图片从临时路径移动到upload
	$result = move_uploaded_file($tmp_name, "./upload/".$imagename);

	//图片在服务器中的路径
	$image="/upload/".$imagename;

	//准备sql
	$sql="insert into users values(null,'$username',$age,$sex,'$image')";
	// var_dump($sql);exit();

	
	//执行添加
	$result= mysqli_query($link, $sql);
	if($result){
		header("location:/index.php");
	}
}

?>

<html>
	<head>
		<title></title>
		<link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
	</head>
	<body>
		<div class="container">
			<h1>添加</h1>

			<form class="col-lg-6" action="./add.php" method="post"  enctype="multipart/form-data">
				
				<div class="form-group">
					<label for="">用户名</label>
					<input type="text" class="form-control" name="username" />
				</div>
				<div class="form-group">
					<label for="">年龄</label>
					<input type="text"  class="form-control" name="age" />
				</div>
				<div class="form-group">
					<label for="">性别</label>
					<div class="radio">
						<label>
							<input type="radio" name="sex" checked value="1" />男
						</label>
					</div>
					
					<div class="radio">
						<label>
							<input type="radio" name="sex" value="2" />女
						</label>
					</div>
					
				</div>
				<div class="form-group">
					<label>图片</label>
					<input type="file" name="image" />
				</div>
				<button class="btn btn-success">提交</button>
			</form>	

		</div>
			
	</body>
</html>


